﻿/// <reference name="MicrosoftAjax.js" />
// Register the namespace for the control.
Type.registerNamespace('Medianamik');

Medianamik.PathPicker = function Medianamik$PathPicker(element) {
    Medianamik.PathPicker.initializeBase(this, [element]);

    this._pathTextBox = null;
    this._hiddenPathField = null;
    this._editionCultureName = null;
    this._linkPickerWindowTitle = null;
    this._opener = null;

    this._getPathForNodeIdError = function(error) {
        alert("Service Error:" + error.get_message());
    };

    this._getPathForNodeIdResult = function(result, info) {
        if (info.requestSucceededCallback && typeof (info.requestSucceededCallback) == 'function') {
            info.requestSucceededCallback(result, info);
        }
    };

    this._requestSuccededCallback = function(result, info) {
        if (result.length > 0) {
            /* pathTextBox */
            $(this._pathTextBox).val(result).attr('onfocus', 'this.blur();').attr('title', result).addClass('ReadOnly');
            /* hiddenPathField */
            $(this._hiddenPathField).val(info.href);
        }
    };

}

function Medianamik$PathPicker$get_pathTextBox() {
    return this._pathTextBox;
}

function Medianamik$PathPicker$set_pathTextBox(element) {
    this._pathTextBox = element;
}

function Medianamik$PathPicker$get_hiddenPathField() {
    return this._hiddenPathField;
}

function Medianamik$PathPicker$set_hiddenPathField(element) {
    this._hiddenPathField = element;
}

function Medianamik$PathPicker$get_editionCultureName() {
    return this._editionCultureName;
}

function Medianamik$PathPicker$set_editionCultureName(value) {
    this._editionCultureName = value;
}

function Medianamik$PathPicker$get_linkPickerWindowTitle() {
    return this._linkPickerWindowTitle;
}

function Medianamik$PathPicker$set_linkPickerWindowTitle(value) {
    this._linkPickerWindowTitle = value;
}


function Medianamik$PathPicker$initialize() {
    Medianamik.PathPicker.callBaseMethod(this, 'initialize');
}

function Medianamik$PathPicker$dispose() {
    $clearHandlers(this.get_element());
    Medianamik.PathPicker.callBaseMethod(this, 'dispose');
}

function Medianamik$PathPicker$getPathForNodeId(nodeid, culturename, requestSucceededCallback) {

};

function Medianamik$PathPicker$documentManagerCallBack(sender, args) {
    if (args) { /*args is undefined if cancelled*/
        var path = args.get_value().pathname;
        
        // 19-10-2009 Jeremy Fabre : quick fix pour IE
        if (path.length > 0 && path.substring(0, 1) != '/')
            path = '/' + path;
            
        /* pathTextBox */
        $(this._pathTextBox).val(path).removeAttr('onfocus').removeAttr('title').removeClass('ReadOnly');
        /* hiddenPathField */
        
        $(this._hiddenPathField).val(path);
    }
}

function Medianamik$PathPicker$medianamikLinkCallBack(sender, argsWrapper) {
    if (argsWrapper) {
        var args = argsWrapper.get_argument();
        if (args) { /*args is undefined if cancelled*/
            Medianamik.UI.Services.UrlsService.GetUrl(args.nodeid, args.culture == '' ? this._editionCultureName : args.culture, args.anchor, args.protocol,
            Function.createDelegate(this, this._getPathForNodeIdResult),
            Function.createDelegate(this, this._getPathForNodeIdError),
            { nodeid: args.nodeid, href: args.href, requestSucceededCallback: Function.createDelegate(this, this._requestSuccededCallback) });
        }
    }
}

function Medianamik$PathPicker$clear() {
    $([this._pathTextBox, this._hiddenPathField]).removeAttr('title').removeAttr('onfocus').removeClass('ReadOnly');
    $([this._pathTextBox, this._hiddenPathField]).val('');
}
function Medianamik$PathPicker$openDocManager() {
    var args = new Telerik.Web.UI.EditorCommandEventArgs("DocumentManager", null, document.createElement("a"));
    args.CssClasses = [];

    this._opener.open('DocumentManager', args);
}

function Medianamik$PathPicker$openMedianamikLinkWindow() {
    var wnd = radopen('/medianamik/modalwindows/MedianamikLinkPickerWindow.aspx?href=' + this.get_hiddenPathField().value, null);
    wnd.set_behaviors(Telerik.Web.UI.WindowBehaviors.Close);
    wnd.setSize(340, 500);
    wnd.set_visibleStatusbar(false);
    wnd.set_modal(true);
    wnd.set_title(this.get_linkPickerWindowTitle());
    wnd.add_close(Function.createDelegate(this, this.medianamikLinkCallBack));

}

function Medianamik$PathPicker$get_opener() {
    return this._opener;
}

function Medianamik$PathPicker$set_opener(component) {
    this._opener = component;
}

Medianamik.PathPicker.prototype = {
    initialize: Medianamik$PathPicker$initialize,
    dispose: Medianamik$PathPicker$dispose,
    documentManagerCallBack: Medianamik$PathPicker$documentManagerCallBack,
    medianamikLinkCallBack: Medianamik$PathPicker$medianamikLinkCallBack,
    clear: Medianamik$PathPicker$clear,
    get_pathTextBox: Medianamik$PathPicker$get_pathTextBox,
    set_pathTextBox: Medianamik$PathPicker$set_pathTextBox,
    get_hiddenPathField: Medianamik$PathPicker$get_hiddenPathField,
    set_hiddenPathField: Medianamik$PathPicker$set_hiddenPathField,
    get_editionCultureName: Medianamik$PathPicker$get_editionCultureName,
    set_editionCultureName: Medianamik$PathPicker$set_editionCultureName,
    get_linkPickerWindowTitle: Medianamik$PathPicker$get_linkPickerWindowTitle,
    set_linkPickerWindowTitle: Medianamik$PathPicker$set_linkPickerWindowTitle,
    get_opener: Medianamik$PathPicker$get_opener,
    set_opener: Medianamik$PathPicker$set_opener,
    openDocManager: Medianamik$PathPicker$openDocManager,
    openMedianamikLinkWindow: Medianamik$PathPicker$openMedianamikLinkWindow
}

// Register the class as a type that inherits from Sys.UI.Control.
Medianamik.PathPicker.registerClass('Medianamik.PathPicker', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();